package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.edu.modules.oa.entity.VeFileFilesEntity;
import org.edu.modules.oa.entity.VeOaMeetSummaryEntity;
import org.edu.modules.oa.mapper.IVeFileFilesMapper;
import org.edu.modules.oa.mapper.IVeOaMeetSummaryMapper;
import org.edu.modules.oa.service.IVeOaMeetSummaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;

@Service
public class VeOaMeetSummaryServiceImpl implements IVeOaMeetSummaryService {

    @Resource
    private IVeOaMeetSummaryMapper veOaMeetSummaryMapper;
    @Resource
    private IVeFileFilesMapper veFileFilesMapper;
    //初始化纪要页面的表格
    public HashMap<String, Object> loadTable(Integer start,Integer pageSize,String title,Integer meetId) {
        QueryWrapper<VeOaMeetSummaryEntity> wrapper = new QueryWrapper();
        IPage<VeOaMeetSummaryEntity> page = new Page<>(start,pageSize);
        if(null != title && !"".equals(title)){
            wrapper.like("title",title);
        }
        wrapper.eq("meet_id", meetId);
        wrapper.orderByDesc("create_time");
        page = veOaMeetSummaryMapper.selectPage(page,wrapper);
        List<VeOaMeetSummaryEntity> list = page.getRecords();
        Integer count = veOaMeetSummaryMapper.selectCount(wrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("count",count);
        return map;
    }

    //删除一条纪要
    public int deleteSummaryById(Integer id) {
        int res = veOaMeetSummaryMapper.deleteById(id);
        return res;
    }

    //修改纪要----------这里要开启事务
    @Transactional(rollbackOn = Exception.class)
    public int updateSummary(VeOaMeetSummaryEntity veOaMeetSummaryEntity, VeFileFilesEntity veFileFilesEntity) {
        int res = 0;
        if(veFileFilesEntity == null){
            res = veOaMeetSummaryMapper.updateById(veOaMeetSummaryEntity);
        }
        if(veFileFilesEntity != null){
            int add = veFileFilesMapper.insert(veFileFilesEntity);
            if(add != 0){
            veOaMeetSummaryEntity.setFileId(veFileFilesEntity.getId());
            res = veOaMeetSummaryMapper.updateById(veOaMeetSummaryEntity);
            }
        }
        return res;
    }

    //添加纪要的方法------有上传文件的话需要开启事务锁同步插入两条消息
    @Transactional(rollbackOn = Exception.class)
    public int insertSummary(VeOaMeetSummaryEntity veOaMeetSummaryEntity, VeFileFilesEntity veFileFilesEntity) {
        int res = 0;
        if(veFileFilesEntity == null){
            res = veOaMeetSummaryMapper.insert(veOaMeetSummaryEntity);
        }
        if(veFileFilesEntity != null){
            int add = veFileFilesMapper.insert(veFileFilesEntity);
            if(add != 0){
            veOaMeetSummaryEntity.setFileId(veFileFilesEntity.getId());
            res = veOaMeetSummaryMapper.insert(veOaMeetSummaryEntity);
            }
        }
        return res;
    }
}
